/*
 * @Author: jiangyg email@jiangyg.com
 * @Date: 2022-11-12 05:49:58
 * @LastEditors: jiangyg email@jiangyg.com
 * @LastEditTime: 2022-11-20 21:10:52
 * @FilePath: /frontend/mall-frontend/src/utils/admin.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import http from "@/utils/httpRequest";
import { ElMessage, ElMessageBox } from "element-plus";

export default {
  /**
   * 设置/获取用户信息
   * @param {*} userInfo
   * @param {*} deserialize
   * @returns
   */
  userInfo(userInfo = undefined, deserialize = true) {
    if (!userInfo) {
      let data = localStorage.getItem("admin.userInfo");
      return deserialize ? JSON.parse(data) : data;
    }
    localStorage.setItem("admin.userInfo", JSON.stringify(userInfo));
  },

  /**
   * 设置/获取动态菜单列表
   * @param {*} menuList
   * @param {*} deserialize
   * @returns
   */
  menuList(menuList = undefined, deserialize = true) {
    if (!menuList) {
      let data = localStorage.getItem("admin.menuList");
      return deserialize ? JSON.parse(data) : data;
    }
    localStorage.setItem("admin.menuList", JSON.stringify(menuList));
  },

  /**
   * 设置/获取动态菜单路由列表
   * @param {*} menuList
   * @param {*} deserialize
   * @returns
   */
  dynamicRouteList(routeList = undefined, deserialize = true) {
    if (!routeList) {
      let data = localStorage.getItem("admin.dynamicRouteList");
      return deserialize ? JSON.parse(data) : data;
    }
    localStorage.setItem("admin.dynamicRouteList", JSON.stringify(routeList));
  },

  /**
   * 设置/获取权限列表
   * @param {*} permissions
   * @param {*} deserialize
   * @returns
   */
  permissions(permissions = undefined, deserialize = true) {
    if (!permissions) {
      let data = localStorage.getItem("admin.permissions");
      return deserialize ? JSON.parse(data) : data;
    }
    localStorage.setItem("admin.permissions", JSON.stringify(permissions));
  },

  /**
   * 是否有权限
   * @param {*} key
   */
  isAuth(key) {
    return (this.permissions() || []).indexOf(key) !== -1 || false;
  },

  /**
   * 表格必须选中一条数据
   * @param selection 表格选中数据
   * @returns {boolean} 是否选中
   */
  obtainOneSelectionTableData(dataTable) {
    let selection = dataTable.getSelection();
    if (!selection || selection.length !== 1) {
      ElMessage.error("请选择一条要操作的数据！");
      return undefined;
    }
    return selection[0];
  },

  /**
   * 删除表格选中数据
   * @param dataTable 表格
   * @param url 请求地址
   * @param success 成功回调删除
   * @param key 默认主键
   * @param error 失败回调函数
   */
  batchDeleteSelectionTableData(dataTable, url, success = undefined, key = "id", error = undefined) {
    let selection = dataTable.getSelection();
    if (!selection || selection.length <= 0) {
      ElMessage.error("请选择要删除的数据！");
      return;
    }
    let deleteIds = [];
    for (let i = 0; i < selection.length; i++) {
      deleteIds.push(selection[i][key]);
    }
    ElMessageBox
      .confirm(`确定对选中[${deleteIds.length}]条数据进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        http
          .instance({
            url: http.adornUrl(url),
            method: "post",
            data: http.adornData(deleteIds, false),
          })
          .then(({ data }) => {
            if (http.success(data)) {
              success ? success(data) : dataTable.loadTableData();
            } else {
              if (error) {
                error(data);
              }
            }
          });
      })
      .catch(()=>{});
  },

  /**
   * 清除登录信息
   */
  clearLoginInfo() {
    localStorage.removeItem("admin.userInfo");
    localStorage.removeItem("admin.menuList");
    localStorage.removeItem("admin.permissions");
  },
};
